package com.jbp.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.domin.LuckReq2;
import com.jbp.model.*;
import com.jbp.redis.RedisConfigService;
import com.jbp.service.*;
import com.jbp.utils.MobileUtil;
import com.jbp.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;

/**机器人任务*/
@Component
@Configuration
public class RobJob {

    @Autowired
    private LuckVUtil luckVUtil;
    @Autowired
    private PoolService poolService;
    @Autowired
    private PushService pushService;
    @Autowired
    private GiftService giftService;
    @Autowired
    private AppConfigService appConfigService;
    @Autowired
    private UserCreatorService userCreatorService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private TecentMsgService tecentMsgService;
    @Value("${xiaohao.namePrex}")
    private String namePrex;
    @Value("${istest}")
    private String istest;

    @Autowired
    private RedisConfigService redisConfigService;

    /*@Scheduled(fixedDelay = 2000)
    public void rob0(){
        startRob(5);
    }

    @Scheduled(fixedDelay = 2000)
    public void rob1(){
        startRob(10);
    }*/

    @Scheduled(fixedDelay = 2000)
    public void rob2(){
        startRob(20);
    }

    /*@Scheduled(fixedDelay = 2000)
    public void rob21(){
        startRob(40);
    }*/

    @Scheduled(fixedDelay = 2000)
    public void rob3(){
        startRob(50);
    }

    @Scheduled(fixedDelay = 2000)
    public void rob4(){
        startRob(100);
    }

    /*@Scheduled(fixedDelay = 2000)
    public void rob5(){
        startRob(200);
    }*/

    @Scheduled(fixedDelay = 2000)
    public void rob7(){
        startRob(300);
    }

    /*@Scheduled(fixedDelay = 2000)
    public void rob6(){
        startRob(1000);
    }*/

    private void startRob(int price){
        if (!"1".equals(appConfigService.getV("lottery"))) {
            return;
        }

        int robCount = appConfigService.getInt("rob"+price);

        if (robCount<=0) {
            return ;
        }

        int times = 1;
        /*if (price<=10) {
            times = 100;
        } else if (price<=50) {
            times = 10;
        }*/
        QueryWrapper<AppConfig> wrapper = new QueryWrapper<>();
        wrapper.like("param_key", "rob");
        wrapper.eq("param_value", "1");
        wrapper.eq("state", "1");
        List<AppConfig> appConfigList = appConfigService.list(wrapper);
        //抽奖机器人数量处理
        int counts = 1;
        if (null != appConfigList && appConfigList.size() > 0) {
            int size = appConfigList.size();
            if (size == 1) {//中奖个数
                counts = 1;
            }else if (size == 2) {
                counts = 1;
            }else if (size == 3) {
                counts = 1;
            }else if (size == 4) {
                counts = 1;
            }else {
                counts = 1;
            }
        }

        robCount = 60;
        for (int j = 0; j < counts; j++) {
            int rand = new Random().nextInt(robCount);
            for (int i = 0; i < robCount; i++) {
                if (i == rand) {
                    int userId = getRober(price, i);
                    if (userId!=0) {
                        LuckReq2 req = new LuckReq2();
                        req.setPrice(price);
                        req.setUserId(userId);
                        req.setTimes(times);
                        req.setRoomId(0);
                        req.setType(2);
                        robLuck(req,counts);
                    }
                }
            }
        }

    }

    private void robLuck(LuckReq2 req, int counts) {
        Pool pool = poolService.getRobPool(req);

        List<Gift> gifts = luckVUtil.luckRob(req, pool, 0);
        Map<Integer,Integer> map=new HashMap<>();
        for (Gift gift : gifts) {
            Integer giftId = gift.getId();
            /*if (map.containsKey(giftId)){
                map.put(giftId,map.get(giftId)+1);
            }else{
                map.put(giftId,1);
            }*/
            //机器人 出的礼物不能X4 只能让他X1
            map.put(giftId,1);
        }

        List<Gift> giftList = new ArrayList<>();
        for (Integer giftId : map.keySet()) {
            Gift gift=giftService.getById(giftId);
            gift.setNum(map.get(giftId));
            giftList.add(gift);
        }
        pushService.bigLuckyRob(req.getUserId(), req.getPrice(), giftList, counts);
    }

    public int getRober(int price, int index) {
        String key = "rob:"+price+":"+index;
        int userId = redisConfigService.getInt(key);
        if (userId==0) {
            User user = createRober();
            if (user==null) {
                return 0;
            }
            userId = user.getId();
            redisConfigService.set(key, userId, (1+ new Random().nextInt(3)) * 60L);
        }
        return userId;
    }

    public User createRober(){
//        String chars = "abcdefghijklmnopkrstuvwxyz";
//        Random rand = new Random();//生成随机数
        String name = namePrex;// + chars.charAt(rand.nextInt(chars.length()));

        String phone = MobileUtil.getMobile();
        User user = userCreatorService.createRob(phone, name);

        if (user!=null) {
            Wallet wallet = new Wallet();
            wallet.setCreateTime(new Date());
            wallet.setUserId(user.getId());
            wallet.setXinzuan(BigDecimal.valueOf(0));
            wallet.setUpdateTime(new Date());
            walletService.save(wallet);

            if (!"test".equals(istest)) {
                tecentMsgService.addUser(user.getId(), false);
            }
        }
        return user;
    }
}
